package arcane.object.gui;

import java.awt.Color;
import java.awt.Graphics2D;

import org.lwjgl.opengl.GL11;

import arcane.constant.Constants;
import arcane.object.game.sprite.MenuSprite;

import com.golden.gamedev.object.GameFont;

public class DefaultLabel extends MenuSprite {
	// BitmapFont used to render label's text
	private GameFont font;

	/**
	 * This labels text.
	 */
	protected String text;

	/**
	 * Constructs a new DefaultLabel with an empty string for text, and a text
	 * color of white at location (0, 0)
	 */
	public DefaultLabel() {
		this("");
	}

	/**
	 * Constructs a new DefaultLabel with specified text, and a texture color of
	 * white at location (0, 0)
	 * 
	 * @param text
	 *            specified text
	 */
	public DefaultLabel(String text) {
		this(text, Color.WHITE);
	}

	/**
	 * Constructs a new DefaultLabel of specified text and color.
	 * 
	 * @param txt
	 *            specified text
	 * @param color
	 *            color of text
	 */
	public DefaultLabel(String txt, Color color) {
		this(txt, color, 0, 0);
	}

	/**
	 * Convenience constructor for hexadecimal color values. Equivalent to
	 * calling new DefaultLabel(text, new Color(hexColor), x, y).
	 * 
	 * Constructs a new DefaultLabel with specified text, and color at a
	 * specified position.
	 * 
	 * @param txt
	 *            text of this label
	 * @param hexColor
	 *            hexadecimal color value of this label's text
	 * @param x
	 *            x location
	 * @param y
	 *            y location
	 */
	public DefaultLabel(String txt, int hexColor, int x, int y) {
		this(txt, new Color(hexColor), x, y);
	}

	/**
	 * Constructs a new DefaultLabel of specified text, and color at a specific
	 * position.
	 * 
	 * @param txt
	 *            specified text
	 * @param color
	 *            color of text
	 * @param x
	 *            x location
	 * @param y
	 *            y location
	 */
	public DefaultLabel(String txt, Color color, int x, int y) {
		super(null, 0, 0);
		font = Constants.fontManager.getFont("MenuFont");
		text = command = txt;
		char[] ary = text.toCharArray();
		width = 0;
		for (int i = 0; i < ary.length; i++) {
			width += font.getWidth(ary[i]);
		}
		setColor(color);
		height = font.getHeight();
		setLocation(x, y);
	}

	/**
	 * Sets this DefaultLabel's text.
	 * 
	 * @param txt
	 *            label's new text
	 */
	public void setText(String txt) {
		text = txt;
	}

	/**
	 * Returns this DefaultLabel's text.
	 * 
	 * @return this label's text
	 */
	public String getText() {
		return text;
	}

	/**
	 * Sets this DefaultLabel's GameFont, used in rendering this label's text.
	 * 
	 * @param font
	 *            this label's new GameFont
	 */
	public void setFont(GameFont font) {
		this.font = font;
	}

	/**
	 * Returns this DefaultLabel's GameFont, used in rendering this label's
	 * text.
	 * 
	 * @return this label's GameFont
	 */
	public GameFont getFont() {
		return font;
	}

	public void render(Graphics2D g, int x, int y) {
		super.render(g, x, y);
		drawText(g, x, y);
	}

	/**
	 * Draws this labels text in its specified color. Since each character in
	 * the GameFont is an image, its essentially a texture because of the LWJGL
	 * "behind-the-scenes" implementation of GTGE. So, the texture can be
	 * rendered with different colors, which means the font can change colors
	 * quickly.
	 * 
	 * @param g
	 *            specific graphics context
	 * @param x
	 *            x location to render text at
	 * @param y
	 *            y location to render text at
	 */
	protected void drawText(Graphics2D g, int x, int y) {
		this.setOpenGLColor(color, alpha);
		font.drawString(g, text, x, y);
	}

	/**
	 * Returns a String representation of this label. Used in debugging.
	 */
	public String toString() {
		return "Label: " + text;
	}
}